package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Composition;
import com.bean.Recipe;
import com.bean.User;
import com.bean.UserInfo;
import com.dao.*;
import com.service.MD5;
import com.service.RecipeCalculate;


@Controller

public class HealController {
//	@Autowired
//	private UserMapper userDao;
	@Autowired
	private RecipeMapper recipeDao ;
	@Autowired
	private CompositionMapper compositionDao;
	
	//在服务器端计算食谱 的营养
	@RequestMapping("/jsp/recipe")
	@ResponseBody
	public Composition ajaxTotal(@RequestBody Map<String,Double> map){
		Composition  result =RecipeCalculate.getAllNutrient(map, compositionDao);
		return result;
		
	}
	
	
	@RequestMapping("/jsp/searchFood")//前端ajax查询食品成分
	@ResponseBody
	public List<Composition> searchFood(String key){
		if(key=="")
			return null;
		else if("ALL".equals(key))
			key="";
		List<Composition> list=compositionDao.getAll(key);
		return list;
	}
	
	//储存食谱
	@RequestMapping("/jsp/saveRecipe")
	@ResponseBody
	public String storeRecipe(String content,String name,String description,HttpServletRequest request){
		HttpSession session=request.getSession();
		int uid=((UserInfo) session.getAttribute("pass")).getId();
		Recipe recipe=new Recipe();
		recipe.setUid(uid);
		recipe.setDescription(description);
		recipe.setName(name);
		recipe.setContent(content);
		recipeDao.add(recipe);
		return "true";
	}
}
